package com.mdb.open.trade.utils;

import com.google.common.collect.Lists;
import com.mdb.bc.utils.BeanConvertUtil;
import com.mdb.open.common.enums.trade.type.AttachFileTypeEnum;
import com.mdb.open.common.enums.trade.type.FileGroupTypeEnum;
import com.mdb.open.trade.domain.bo.trade.GetTradeFileAttachBo;
import com.mdb.open.trade.domain.bo.trade.QueryAttachListByNoAndTypeListBo;
import com.mdb.open.trade.domain.po.trade.AttachFile;
import com.mdb.open.trade.domain.vo.trade.FileInfoVo;
import com.mdb.open.trade.domain.vo.trade.LoanApplyListAttachInfoVo;
import com.mdb.open.trade.domain.vo.trade.TradeFileListAttachInfoVo;
import com.mdb.open.trade.service.trade.TfAttachFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: you.yichao
 * @create: 2019-08-10 18:54
 **/
@Component
public class ConvertFileVoUtil {

    @Autowired
    TfAttachFileService tfAttachFileService;

    /**
     * 融资档案相关
     */
    public List<TradeFileListAttachInfoVo> convert(GetTradeFileAttachBo bo) {

        Map<AttachFileTypeEnum, List<AttachFile>> collect = getAttachFileMapByTypeEnum(bo);

        List<TradeFileListAttachInfoVo> infoVoList = Lists.newArrayList();
        TradeFileListAttachInfoVo infoVo = null;
        Iterator<Map.Entry<AttachFileTypeEnum, List<AttachFile>>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<AttachFileTypeEnum, List<AttachFile>> entry = iterator.next();
            infoVo = new TradeFileListAttachInfoVo();
            infoVo.setAttachFileTypeEnum(entry.getKey());
            infoVo.setAllowNull(entry.getKey().isAllowNull());
            infoVo.setFileInfoVoList(BeanConvertUtil.convertList(entry.getValue(), file -> {
                FileInfoVo vo = new FileInfoVo();
                vo.setFileUrl(file.getFileUrl());
                vo.setFileName(file.getFileName());
                vo.setFileType(file.getType());
                vo.setAttachNo(file.getAttachNo());
                vo.setStatus(file.getStatus());
                return vo;
            }));
            infoVoList.add(infoVo);

        }
        return infoVoList;
    }

    /**
     * 融资申请相关
     */
    public List<LoanApplyListAttachInfoVo> convertLoanApply(GetTradeFileAttachBo bo) {

        Map<AttachFileTypeEnum, List<AttachFile>> collect = getAttachFileMapByTypeEnum(bo);

        List<LoanApplyListAttachInfoVo> infoVoList = Lists.newArrayList();
        LoanApplyListAttachInfoVo infoVo = null;
        Iterator<Map.Entry<AttachFileTypeEnum, List<AttachFile>>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<AttachFileTypeEnum, List<AttachFile>> entry = iterator.next();
            infoVo = new LoanApplyListAttachInfoVo();
            infoVo.setAttachFileTypeEnum(entry.getKey());
            infoVo.setAllowNull(entry.getKey().isAllowNull());
            infoVo.setFileInfoVoList(BeanConvertUtil.convertList(entry.getValue(), file -> {
                FileInfoVo vo = new FileInfoVo();
                vo.setFileUrl(file.getFileUrl());
                vo.setFileName(file.getFileName());
                vo.setFileType(file.getType());
                vo.setAttachNo(file.getAttachNo());
                vo.setStatus(file.getStatus());
                return vo;
            }));
            infoVoList.add(infoVo);

        }
        return infoVoList;
    }

    /**
     * 获取文件分组
     */
    private Map<AttachFileTypeEnum, List<AttachFile>> getAttachFileMapByTypeEnum(GetTradeFileAttachBo bo) {
        QueryAttachListByNoAndTypeListBo queryFileBo = new QueryAttachListByNoAndTypeListBo();
        queryFileBo.setApplyOrderNo(bo.getApplyOrderNo());
        List<AttachFileTypeEnum> list = AttachFileTypeEnum.findByGroup(FileGroupTypeEnum.APPLY_ORDER);
        queryFileBo.setType(list);
        queryFileBo.setCustomerCode(bo.getCustomerCode());
        queryFileBo.setStatus(bo.getStatus());

        List<AttachFile> resultList = tfAttachFileService.queryAttachByNoAndTypeList(queryFileBo);
        return resultList.stream().collect(Collectors.groupingBy(AttachFile::getType));
    }

}
